<?php
namespace app\admin\controller\v1;

use app\admin\controller\Fun;
use app\common\Model\BannerType;
use app\admin\service\BannerTypeService;
use think\Db;

/**
 * @title 后台-捎货管理-banner管理-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class Banner extends Base
{
    /**
     * @title 可用banner类型(下拉框使用)
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:bannerType desc:可用banner类型(下拉框使用)
     *
     * @return data:[]
     */
    public function bannerType()
    {
        $where = ['status' => \app\common\Model\FuncClass::STATUS_NORMAL];

        $list = Db::name('banner_type')
            ->where($where)
            ->field('id,name')
            ->select();
        if ($list) {
            return Fun::success($list);
        } else {
            return Fun::error('333333');
        }
    }   

    /**
     * @title banner列表
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:bannerList other:bannerList desc:banner列表
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序,1倒序
     *
     * @return data:[]
     */
    public function bannerList()
    {
        $where['b.is_del'] = BannerType::IS_DEL_NORMAL;
        //查询
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['b.name'] = ['like', '%' . $search . '%'];
        }
        $BannerType = new \app\common\Model\BannerType();
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $BannerType->sort_field) && isset($this->param['sort_type'])) {
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['sort' => $this->sort_type[1]];
        }
        $list = Db::name('banner_type b')
            ->join('user u', 'u.uid=b.update_uid', 'left')
            ->where($where)
            ->order($order)
            ->field('u.nick_name,b.*')
            ->paginate($this->page_size, false, ['page' => $this->param['page']]);
        if ($list) {
            return Fun::success(['list' => $list->items(), 'total_num' => $list->total()]);
        } else {
            return Fun::error('333333');
        }
    }

    /**
     * @title banner状态类型 禁用,解禁,删除
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default:alterBannerStatus other:alterBannerStatus desc:banner状态类型 禁用,解禁,删除
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:bannerId
     * @param name:status type:string require:1 default: other:0 desc:用户状态:1为有效,0为禁用,''不改变
     * @param name:is_del type:string require:1 default: other:0 desc:能否提现:1为删除,''不改变
     *
     * @return data:[]
     */
    public function alterBannerStatus()
    {
        $ids = explode(',', trim($this->param['ids'], ','));
        $updata = [];

        //状态:1为有效,0为禁用
        if (isset($this->param['status']) && $this->param['status'] != '') {
            $updata['status'] = $this->param['status'] == 1 ? 1 : 0;
        }

        if (isset($this->param['is_del']) && $this->param['is_del'] != '') {
            $updata['is_del'] = $this->param['is_del'] == 1 ? 1 : 0;
        }
        if (count($updata) && count($ids)) {
            $BannerType = New \app\common\Model\BannerType();
            $res = $BannerType->where('id', 'in', $ids)
                ->update($updata);
            return Fun::success('修改成功');
        }
        return Fun::error('333333');
    }

    /**
     * @title banner排序移动
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:alterBannerSort desc:banner排序移动
     *
     * @param name:ids type:string require:1 default: other:10086,10087,10088 desc:bannerId
     * @param name:sort type:string require:1 default: other:0 desc:状态:1为上升,-1为下降
     *
     * @return data:[]
     */
    public function alterBannerSort()
    {
        $ids = explode(',', trim($this->param['ids'], ','));

        $sort = intval($this->param['sort']) > 0 ? 1 : -1;
        $res = BannerTypeService::alterBannerSort($ids, $sort);

        if ($res == 1) {
            return Fun::success('修改成功');
        } elseif ($res == 2) {
            $txt = $sort == 1 ? '已经是最大值' : '已经是最小值';
            return Fun::error('802001', $txt, 1);
        } else {
            return Fun::error('333333');
        }
    }

    /**
     * @title banner详情查询
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:bannerDetail desc:banner详情查询
     *
     * @param name:id type:string require:1 default: other:1 desc:bannerId
     *
     * @return data:[]
     */
    public function bannerDetail()
    {
        if (empty($this->param['id']) || intval($this->param['id']) <= 0) {
            return Fun::error('999994');
        }
        $where['is_del'] = BannerType::IS_DEL_NORMAL;
        $where['id'] = intval($this->param['id']);
        $detail = Db::name('banner_type')
            ->where($where)
            ->find();
        if ($detail) {
            return Fun::success($detail);
        } else {
            return Fun::error('333333');
        }
    }

    /**
     * @title 添加新的banner
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addBanner desc:添加新的banner
     *
     * @param name:type type:string require:1 default: other:0,1,2,3 desc:banner类型,0无,1h5,2商品,3店铺
     * @param name:name type:string require:1 default: other: desc:banner名称
     * @param name:pic_url type:string require:1 default: other: desc:图片地址
     * @param name:link type:string require:0 default: other: desc:跳转具体内容:链接地址,商品ID,店铺ID,若没有则传''
     *
     * @return data:[]
     */
    public function addBanner()
    {
        //根据acc_id查询uid
        $user = DB::name('admin_user')->where(['acc_id'=>$this->param['acc_id']])->field('uid')->find(); 
        $uid = $user['uid'];
        $sort = \app\common\Model\BannerType::max('sort');

        $data = [
            'create_uid'=>$uid,
            'type'=>$this->param['type'],
            'name' => $this->param['name'],
            'sort' => $sort + 1,
            'pic_url' => $this->param['pic_url'],
            'link' => $this->param['link'],
            'status' => 0,
            'update_uid' => $uid,
            'create_time' => date('Y-m-d H:i:s',time()),
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        // 启动事务
        Db::startTrans();
        try {
            $res = Db::name('banner_type')
                ->insert($data);
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

    /**
     * @title 编辑banner
     * @description
     * @author saving
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Banner desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:editBanner desc:编辑banner
     *
     * @param name:id type:string require:1 default: other:1 desc:bannerId
     * @param name:type type:string require:1 default: other:0,1,2,3 desc:banner类型,0无,1h5,2商品,3店铺
     * @param name:name type:string require:1 default: other: desc:banner名称
     * @param name:pic_url type:string require:1 default: other: desc:图片地址
     * @param name:link type:string require:0 default: other: desc:跳转具体内容:链接地址,商品ID,店铺ID,若没有则传''
     *
     * @return data:[]
     */
    public function editBanner()
    {
        if (empty($this->param['id']) || empty($this->param['name'])) {
            return Fun::error('999994');
        }
        //根据acc_id查询uid
        $user = DB::name('admin_user')->where(['acc_id'=>$this->param['acc_id']])->field('uid')->find(); 
        $uid = $user['uid'];

        $BannerType = new \app\common\Model\BannerType();
        $res = $BannerType->where('id', $this->param['id'])->find();
        if (!$res) {
            return Fun::error('333333');
        }

        // 启动事务
        Db::startTrans();
        try {
            $res->type = $this->param['type'];
            $res->name = $this->param['name'];
            $res->pic_url = $this->param['pic_url'];
            $res->link = $this->param['link'];
            $res->update_time = date('Y-m-d H:i:s',time());
            $res->update_uid = $uid;
            $res->save();
            // 提交事务
            Db::commit();
            return Fun::success('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return Fun::error('333332');
        }
    }

}